<?php


function awords($subject){
    preg_match_all("/[А-Яа-я\wрстуфхцчшщьыъэю’\']+/", $subject, $match);
    
    $words = $match[0];
    
    $hrefs = array();
    
    foreach ($words as $word) {
        $hrefs[]= "<a title=\"Поиск по слову '$word'\" class=\"ahide\" href='./searchMe.php?searchQuery=$word'>$word</a>";
    }

    return implode(" ", $hrefs);
    
}

require_once 'Dsql.php';

$db = new Dsql("mysql-88.ukraine.com.ua", "barthez_ai", "x9gedv4w");
$db->selectDb('barthez_ai');
$db->selectCharset('utf8');

$queryString = $_GET['searchQuery'];



$words = preg_match_all("/[А-Яа-я\wрстуфхцчшщьыъэю’\']+/", $queryString, $match);
$match = $match[0];

$allResultSet = array();

$query = array();

$fields = array("a.text", "a.theme");

$method = "OR";
$query = array();
foreach ($fields as $field) {
    $tmpQuery = array();
    foreach ($match as $one) {
        $tmpQuery [] = "($field LIKE '%" . $one . "%')";
    }
    $query []= implode("AND", $tmpQuery);
    
    
}
$query = "(" . implode($method, $query) . ")";

$allResultSet = $db->createQuery("SELECT * 
                             FROM `byone` a 
                             WHERE (a.text > '') AND $query LIMIT 500")
//              ->getSql();
//        print "<pre>";
//        print_r($allResultSet);
//        print "</pre>";
//        die("++");
        ->getResult();
?>


<style>
    .searchhl{
        color: blue;
    } 
</style>

<? include 'mainHeader.php';?>

<div class="margin-auto" style="width: 780px;">
    <div class="mainContainer">
    <? include 'header.php';?>

    <? if (count($allResultSet)): ?>    
            
            <div class="w-750 margin-auto tal-center">
                <span class="a-blue f18">Вы искали: </span>  
                <span class="f18">"<?= $queryString?>"</span>
                <span class="f18">Найдено:</span>
                <span class="f18 a-blue"><?= count($allResultSet)?></span>
                <span class="f18">результатов</span>
                
            </div>

        <? foreach ($allResultSet as $one): ?>
                    <div class="w-660 f14 f-Ver pm-8  bg-yellow">
                        <a title="Найти по автору <?= $one['theme'] ?>  " href="./searchMe.php?searchQuery=<?= $one['theme'] ?>"><?= $one['theme'] ?></a>:
                        <?= awords($one['text']) ?>
                        <div class="w-300 margin-auto" >
                            <span  class="f10 c-gray">скачать картинку:</span>
                            <? for($i=1; $i<=4; $i++):?>
                                <a class="asJpeg hm10 a-grayBg" href="./img-<?=rand(1000,9999)?>.png?text=<?= $one['theme'] ?>: <?=$one['text']?>&font=<?=$i ?>" title="скачать картинку с текстом:'<?= $one['text']?>'"><?=$i?></a>
                            <? endfor;?>
                        </div>

                        
                    </div>
        <? endforeach; ?>
    <? else: ?>
        <div class="w-660 f14 f-Ver pm-8">
            <h1>Удивительно!</h1>
            <span class="f18">По запросу "<u><?=$queryString?></u>" совсем ничего не найдено. Попробуйте задать другую строку для поиска.</span>
        </div>
    <? endif; ?>
        
    <hr />

    </div>
        <div class="w-750 margin-auto tal-center">
        
        
    </div>

</div>